package com.xjj.controller.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjj.entity.Parent;
import com.xjj.entity.ResponseEntity;
import com.xjj.entity.Student;
import com.xjj.service.StudentService;
import com.xjj.util.utils.JwtUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : 王泰元
 * @date : 2022/10/27 18:09
 */
@RestController
@RequestMapping("/manage/api/student")
public class ApiStudentController {

    @Resource
    StudentService studentService;

    @GetMapping("/byTel/{tel}")
    public ResponseEntity<List<Student>> getByTel(@PathVariable String tel) {
        List<Student> byTel = studentService.getByTel(tel);
        return ResponseEntity.SUCCESS("ok", byTel);
    }

    @GetMapping("/getMyChildren")
    public ResponseEntity<List<Student>> getMyChildren(HttpServletRequest request) throws JsonProcessingException {
        String token = request.getHeader("token");
        Parent parent = JwtUtil.getParent(token);
        List<Student> children = studentService.getByTel(parent.getTel());
        return ResponseEntity.SUCCESS("ok", children);
    }
}
